//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/*
 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Thomas Roell not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Thomas Roell makes no representations
 * about the suitability of this software for any purpose.  It is provided
 * "as is" without express or implied warranty.
 *
 * THOMAS ROELL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THOMAS ROELL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifndef _XF86_H
#define _XF86_H

#include "misc.h"
#include "scrnintstr.h"

#include "xf86_Option.h"

#include "DisplayMode.h"
#if 0
/*
 * structure common for all modes
 */
typedef struct _DispM {
  struct _DispM    *prev,*next;
  char        *name;              /* identifier of this mode */
  /* These are the values that the user sees/provides */
  int        Clock;              /* pixel clock */
  int           HDisplay;           /* horizontal timing */
  int           HSyncStart;
  int           HSyncEnd;
  int           HTotal;
  int           HSkew;
  int           VDisplay;           /* vertical timing */
  int           VSyncStart;
  int           VSyncEnd;
  int           VTotal;
  int           Flags;
  /* These are the values the hardware uses */
  int        SynthClock;         /* Actual clock freq to be programmed */
  int        CrtcHDisplay;
  int        CrtcHSyncStart;
  int        CrtcHSyncEnd;
  int        CrtcHTotal;
  int        CrtcHSkew;
  int        CrtcVDisplay;
  int        CrtcVSyncStart;
  int        CrtcVSyncEnd;
  int        CrtcVTotal;
  _ELASTOS Boolean        CrtcHAdjusted;
  _ELASTOS Boolean        CrtcVAdjusted;
  int        PrivSize;
  Int32     *Private;
} DisplayModeRec, *DisplayModePtr;
#endif

#define V_PHSYNC    0x0001
#define V_NHSYNC    0x0002
#define V_PVSYNC    0x0004
#define V_NVSYNC    0x0008
#define V_INTERLACE 0x0010
#define V_DBLSCAN   0x0020
#define V_CSYNC     0x0040
#define V_PCSYNC    0x0080
#define V_NCSYNC    0x0100
#define V_HSKEW     0x0200    /* hskew provided */
#define V_PIXMUX    0x1000
#define V_DBLCLK    0x2000
#define V_CLKDIV2   0x4000

/* The monitor description */

#define MAX_HSYNC 8
#define MAX_VREFRESH 8

//typedef struct { float hi, lo; } range;
typedef struct { int hi, lo; } range;

typedef struct {
   char *id;
   char *vendor;
   char *model;
//   float EMPTY;
   int EMPTY;
   int n_hsync;
   range hsync[MAX_HSYNC];
   int n_vrefresh;
   range vrefresh[MAX_VREFRESH];
   DisplayModePtr Modes, Last; /* Start and end of monitor's mode list */
} MonRec, *MonPtr;

#define MAXCLOCKS   128
#define MAXDACSPEEDS  4  /* for <= 8, 16, 24, 32bpp */

/* Set default max allowed clock to 90MHz */
#define DEFAULT_MAX_CLOCK    90000

/*
 * the graphic device
 */
typedef struct {
  _ELASTOS Boolean           configured;
  int            tmpIndex;
  int            scrnIndex;
  _ELASTOS Boolean           (* Probe)();
  _ELASTOS Boolean           (* Init)(int,ScreenPtr, int,char **);
  int            (* ValidMode)(DisplayModePtr,_ELASTOS Boolean,int);
  void           (* EnterLeaveVT)(_ELASTOS Boolean, int);
  void           (* EnterLeaveMonitor)();
  void           (* EnterLeaveCursor)();
  void           (* AdjustFrame)(int, int);
  _ELASTOS Boolean           (* SwitchMode)(DisplayModePtr);
  void           (* DPMSSet)(int);//
  void           (* PrintIdent)();
  int            depth;
  xrgb           weight;
  int            bitsPerPixel;
  int            defaultVisual;
  int            virtualX,virtualY;
  int            displayWidth;
  int            frameX0, frameY0, frameX1, frameY1;
  OFlagSet       options;
  OFlagSet       clockOptions;
  OFlagSet       xconfigFlag;
  char           *chipset;
  char           *ramdac;
  int            dacSpeeds[MAXDACSPEEDS];
  int            dacSpeedBpp;
  int            clocks;
  int            clock[MAXCLOCKS];
  int            maxClock;
  int            videoRam;
  int            BIOSbase;                 /* Base address of video BIOS */
  unsigned long  MemBase;                  /* Frame buffer base address */
  int            width, height;            /* real display dimensions */
  unsigned long  speedup;                  /* Use SpeedUp code */
  DisplayModePtr modes;
  MonPtr         monitor;
  char           *clockprog;
  int            textclock;
  _ELASTOS Boolean           bankedMono;      /* display supports banking for mono server */
  char           *name;
  xrgb           blackColour;
  xrgb           whiteColour;
  int            *validTokens;
  char           *patchLevel;
  unsigned int   IObase;          /* AGX - video card I/O reg base        */
  unsigned int   DACbase;         /* AGX - dac I/O reg base               */
  unsigned long  COPbase;         /* AGX - coprocessor memory base        */
  unsigned int   POSbase;         /* AGX - I/O address of POS regs        */
  int            instance;        /* AGX - XGA video card instance number */
  int            s3Madjust;
  int            s3Nadjust;
  int            s3MClk;
  int            chipID;
  int            chipRev;
  unsigned long  VGAbase;         /* AGX - 64K aperture memory address    */
  int            s3RefClk;
  int            s3BlankDelay;
  int            textClockFreq;
  char          *DCConfig;
  char          *DCOptions;
  int            MemClk;          /* General flag used for memory clocking */
  int            LCDClk;
} ScrnInfoRec, *ScrnInfoPtr;

typedef struct {
  int           token;                /* id of the token */
  char          *name;                /* pointer to the LOWERCASED name */
} SymTabRec, *SymTabPtr;

#define VGA_DRIVER  1
#define V256_DRIVER 2
#define WGA_DRIVER  3
#define XGA_DRIVER  4

#define ENTER       1
#define LEAVE       0

/* These are possible return values for xf86CheckMode() and ValidMode() */
#define MODE_OK        0
#define MODE_HSYNC  1        /* hsync out of range */
#define MODE_VSYNC  2        /* vsync out of range */
#define MODE_BAD    255        /* unspecified reason */

/* These are the possible flags for ValidMode */
#define MODE_USED    1    /* this mode is really being used in the */
                /* modes line of the Display Subsection  */
#define MODE_SUGGESTED    2    /* this mode is included in the available*/
                /* modes in the Monitor Section */
#define MODE_VID    3    /* this is called from the VidMode extension */

/* Indicates the level of DPMS support */
typedef enum {
    DPMSSupportUnknown,
    DPMSNotSupported,
    DPMSFullSupport
} DPMSSupportStatus;

/* flags for xf86LookupMode */
#define LOOKUP_DEFAULT        0    /* Use default mode lookup method */
#define LOOKUP_BEST_REFRESH    1    /* Pick modes with best refresh */
#define LOOKUP_NO_INTERLACED    2    /* Ignore interlaced modes */
#define LOOKUP_FORCE_DEFAULT    4    /* Force default lookup method */

#define INTERLACE_REFRESH_WEIGHT    1.5

/* SpeedUp options */

#define SPEEDUP_FILLBOX        1
#define SPEEDUP_FILLRECT    2
#define    SPEEDUP_BITBLT        4
#define SPEEDUP_LINE        8
#define SPEEDUP_TEGBLT8      0x10
#define SPEEDUP_RECTSTIP     0x20

/*
 * This is the routines where SpeedUp is quicker than fXF86.  The problem is
 * that the SpeedUp fillbox is better for drawing vertical and horizontal
 * line segments, and the fXF86 version is significantly better for
 * more general lines
 */
#define SPEEDUP_BEST        (SPEEDUP_FILLRECT | SPEEDUP_BITBLT | \
                 SPEEDUP_LINE | SPEEDUP_TEGBLT8 | \
                 SPEEDUP_RECTSTIP)
/*
#define SPEEDUP_BEST        (SPEEDUP_FILLBOX | SPEEDUP_FILLRECT | \
                 SPEEDUP_BITBLT | SPEEDUP_LINE | \
                                 SPEEDUP_TEGBLT8 | SPEEDUP_RECTSTIP)
*/

/*
 * SpeedUp routines which are not dependent on the screen virtual resolution
 */
#ifndef SPEEDUP_ANYWIDTH
#define SPEEDUP_ANYWIDTH    (SPEEDUP_FILLRECT | SPEEDUP_BITBLT | \
                                 SPEEDUP_LINE | SPEEDUP_FILLBOX)
#endif

/*
 * SpeedUp routines which are not dependent on ET4000
 */
#ifndef SPEEDUP_ANYCHIPSET
#define SPEEDUP_ANYCHIPSET    (SPEEDUP_TEGBLT8 | SPEEDUP_RECTSTIP)
#endif

/* All SpeedUps */
#define SPEEDUP_ALL        (SPEEDUP_FILLBOX | SPEEDUP_FILLRECT | \
                 SPEEDUP_BITBLT | SPEEDUP_LINE | \
                                 SPEEDUP_TEGBLT8 | SPEEDUP_RECTSTIP)

/* SpeedUp flags used if SpeedUp is not in XF86Config */
#define SPEEDUP_DEFAULT        SPEEDUP_ALL

extern _ELASTOS Boolean        xf86VTSema;

/* Global data */
/* xf86Init.c */
extern double xf86rGamma, xf86gGamma, xf86bGamma;

#ifdef XF86VIDMODE
extern _ELASTOS Boolean xf86VidModeEnabled;
extern _ELASTOS Boolean xf86VidModeAllowNonLocal;
#endif
#ifdef XF86MISC
extern _ELASTOS Boolean xf86MiscModInDevEnabled;
extern _ELASTOS Boolean xf86MiscModInDevAllowNonLocal;
#endif

/* PCI probe flags */


typedef enum {
    PCIProbe1 = 0,
    PCIProbe2,
    PCIForceConfig1,
    PCIForceConfig2
} PciProbeType;

extern PciProbeType xf86PCIFlags;

/* Function Prototypes */
#ifndef _NO_XF86_PROTOTYPES

/* xf86Init.c */
void InitOutput(
    ScreenInfo *pScreenInfo
);

void InitInput(
    int argc,
    char **argv
);

void ddxGiveUp(
    void
);

void AbortDDX(
    void
);

int ddxProcessArgument(
    int argc,
    char *argv[],
    int i
);

void ddxUseMsg(
    void
);

/* xf86Config.c */
unsigned int StrToUL(
    char *str
);

void xf86Config(
    int vtopen
);

_ELASTOS Boolean xf86LookupMode(
    DisplayModePtr target,
    ScrnInfoPtr driver,
    int flags
);

void xf86VerifyOptions(
    OFlagSet *allowedOptions,
    ScrnInfoPtr driver
);

int xf86CheckMode(
    ScrnInfoPtr scrp,
    DisplayModePtr dispmp,
    MonPtr monp,
    int verbose
);

int xf86GetNearestClock(
    ScrnInfoPtr Screen,
    int Frequency
);


CARD32 GetTimeInMillis(
    void
);

void OsVendorInit(
    void
);
#endif /* _NO_XF86_PROTOTYPES */

/* End of Prototypes */

#endif /* _XF86_H */


